//
//  MenuListViewController.m
//  OrderMeal
//
//  Created by 宏创 on 14-3-6.
//  Copyright (c) 2014年 周浩. All rights reserved.
//

#import "MenuListViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "GoodDetailViewController.h"
#import "MBProgressHUD.h"
@interface MenuListViewController ()
{
    NSString *_date;               //当前时间
    NSString *_goodsTypeID;        //商品类型
    NSMutableArray *_goodsArray ;  //商品
    NSString *_goodID;             //商品id
    MBProgressHUD *hud;
}

@end

@implementation MenuListViewController
@synthesize regionID = _regionID;
@synthesize buildingArray = _buildingArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

-(void)loadView
{
  [super loadView];
   
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [self.tableView setTableFooterView:view];
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    _date = [dateFormatter stringFromDate:[NSDate date]];
    hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"加载数据中";
    [hud show:YES];
    [self requestWithDate:_date
                  regionId:_regionID
                  goodsTypeId:@"1"];
    
}

-(void)requestWithDate:(NSString *)date
                regionId:(NSString *)regionID
                goodsTypeId:(NSString *)goodsTypeID
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    NSDictionary *parameters=@{
                               @"regionId" :   regionID,
                               @"date"     :   date,
                               @"goodsTypeId" : goodsTypeID
                               };
    [manager GET:regiongoods_url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject[@"success"] isEqualToNumber:@1]) {
            _goodsArray=responseObject[@"retData"];
            [_tableView reloadData];
            [hud removeFromSuperview];
        }else{
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"消息" message:@"获取数据失败" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
            [alert show];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"消息" message:@"获取数据失败" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [alert show];
    }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _goodsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier=@"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
    
    NSDictionary *good =_goodsArray[indexPath.row];
    
    cell.textLabel.text=[NSString stringWithFormat:@"%@",[[good objectForKey:@"goods"] objectForKey:@"name"]];
    cell.detailTextLabel.text= [NSString stringWithFormat:@"¥%@",good[@"goods"][@"finalPrice"]];
    cell.imageView.image=[UIImage imageNamed:@"food.jpg"];
    cell.textLabel.frame = CGRectMake(cell.detailTextLabel.frame.origin.x, cell.imageView.frame.origin.y, cell.detailTextLabel.frame.size.width,20);
//    cell.textLabel.text=[NSString stringWithFormat:@"%@",[good objectForKey:@"apple"]];
    //    cell.detailTextLabel.text=[NSString stringWithFormat:@"%@",[good objectForKey:@"orange"]];
//    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath *indexPath=[self.tableView indexPathForCell:sender];
    _goodID=[[_goodsArray[indexPath.row] objectForKey:@"goods"]objectForKey:@"id"];
    
    GoodDetailViewController *goodDetailVC=segue.destinationViewController;
    goodDetailVC.goodsid=_goodID;
    goodDetailVC.buildingArray=_buildingArray;
    goodDetailVC.limitNum=[_goodsArray[indexPath.row] objectForKey:@"limitNum"] ;
    goodDetailVC.goodInfo = _goodsArray[indexPath.row][@"goods"];
}

- (IBAction)indexChange:(UISegmentedControl *)sender {
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    
    _goodsTypeID=[NSString stringWithFormat:@"%d",sender.selectedSegmentIndex+1];
    
    hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"加载数据中";
    [hud show:YES];
    [self requestWithDate:_date
                regionId:_regionID
             goodsTypeId:_goodsTypeID];
    
    [UIView commitAnimations];
}

@end
